<?php

namespace co2_OS;

function run($app,$version=null)

{
	$app=strtolower($app);

	if(!isset(main::getAppsManifest()->$app))
	throw new \co2\Exception($app.'? App not manifested');

	if(is_null($version))
	$version=main::getAppsManifest()->$app;
	
	$version_ns='v'.preg_replace('/\./','\v',$version);
	$version_fs='v'.preg_replace('/\./','/v',$version);
	
	$file=main::getSysVar('apps').'/'.$app.'/'.$version_fs.'/run.php';
	$conf=main::getSysVar('apps').'/'.$app.'/'.$version_fs.'/conf.json';
	$class='co2_OS\apps\\'.$app.'\\'.$version_ns.'\run';
	
	if(!file_exists($file))
	throw new \co2\Exception('Cannot run app '.$app.' version '.$version.': run file not found');
	
	return new $class;
}
